define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    //edit_url: 'order/edit',
                    // del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'user_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'buyer_id', title: __('Buyer_id')},
                        {field: 'order_no', title: __('Order_no')},
                        // {field: 'trade_no', title: '第三方交易号'},
                        {field: 'order_type', title: __('Order_type'), searchList: {"0":__('Order_type 0'),"1":__('Order_type 1'),"2":__('Order_type 2'),"3":__('Order_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'order_status', title: __('Order_status'), searchList: {"0":__('Order_status 0'),"1":__('Order_status 1'),"2":__('Order_status 2'),"3":__('Order_status 3'),"4":__('Order_status 4')}, formatter: Table.api.formatter.status},
                        {field: 'order_title', title: __('Order_title'), operate:'false', visible: false},
                        // {field: 'user.mobile', title: __('User.mobile')},
                        // {field: 'account_type', title: '收款类型', operate:'false'},
                        // {field: 'account_name', title: '收款用户名', operate:'false'}, 
                        // {field: 'account_number', title: '收款账号', operate:'false'},
                        // {field: 'account_image', title: '收款码', operate:'false', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'coin_type', title:  __('Coin_type'), operate:'false',searchList: {"1":__('Coin_type 1'),"2":__('Coin_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'coin_num', title: '交易数量', operate:'false'},
                        {field: 'sell_price', title: '交易价格', operate:'false'},
                        {field: 'unit_price', title: '交易单价', operate:'false'},
                        {field: 'transfer_fee', title: __('Transfer_fee'), operate:'false'},
                        // {field: 'exchange_rate', title: __('Exchange_rate'), operate:'false', visible: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'checktime', title: __('Checktime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [
                                {   
                                    visible: function (row) {
                                        if(row.order_source==1 && row.order_type==1 && row.order_status==0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'ajax',
                                    text: '驳回订单',
                                    title: '驳回订单',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'order/rebut',
                                    confirm: '确认驳回该订单吗?',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    visible: function (row) {
                                        if(row.order_source==2  && row.order_status==0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'ajax',
                                    text: __('通过'),
                                    title: __('通过'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'order/agree',
                                    confirm: 'FIL3提币确认通过',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    visible: function (row) {
                                        if(row.order_source==2  && row.order_status==0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    name: 'ajax',
                                    text: __('驳回'),
                                    title: __('驳回'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'order/disagree',
                                    confirm: 'FIL3提币驳回',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ], 
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        ajax: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});